%1Enter the table name:
%2Enter the column name:
%TEMPLATE
SET VAR %2 TEXT
SET VAR vxrows INT
SET VAR vxmid INT
SET VAR vxmidmid INT
PROJECT median FROM %1 USING %2 SORTED BY %2
COMPUTE vxrows AS ROWS FROM median
SET VAR vxmid TO (.vxrows / 2)
SET VAR vxmidmid TO (.vxmid + .vxmid)
IF vxmidmid EQ .vxrows THEN
  SET VAR vxmid2 TO (.vxmid + 1)
  SET VAR vxmedlow TO %2 IN median WHERE COUNT EQ .vxmid
  SET VAR vxmedhi TO %2 IN median WHERE COUNT EQ .vxmid2
  SET VAR vxmedian TO (.vxmedlow + .vxmedhi)
  SET VAR vxmedian TO (.vxmedian / 2)
ELSE
  SET VAR vxmid TO (.vxmid + 1)
  SET VAR vxmedian TO %2 IN median WHERE COUNT EQ .vxmid
ENDIF
WRITE "Median value: " AT 15 6
SHOW VAR vxmedian AT 15 19
WRITE "Press any key to continue." AT 21 10
PAUSE
DELETE ROWS FROM median WHERE %2 EXISTS OR %2 FAILS
REMOVE median
CLE vxmid vxmid2 vxmidmid vxrows vxmedlow vxmedhi vxmedian %2
RETURN
 
